﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.crew.ui {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Mechanics.*;
    import Events.*;
    import flash.utils.*;
    import Display.DialogUI.*;
    import org.aswing.*;
    import Classes.*;
    import ZLocalization.*;
    import Display.*;
    import flash.text.*;
    import org.aswing.event.*;
    import Display.RenameUI.*;
    import Display.aswingui.*;
    import Mechanics.GameEventMechanics.*;
    import com.zynga.skelly.util.*;
    import Display.MarketUI.assets.*;
    import flash.external.*;

    public class RollCallDialogView extends GenericDialogView {

        public static const LIST_WIDTH:int = 709;
        public static const LIST_HEIGHT:int = 284;
        public static const REWARD_HEIGHT:int = 135;

        protected var m_mechData:RollCallDataMechanic;
        protected var m_rewardData:TieredDooberMechanic;
        protected var m_data:Array;
        protected var m_timer:Timer;
        private var m_timerText:JLabel;
        protected var m_friendsResponded:int;
        private var m_nextRewardIndex:int = 0;
        private var m_checkinsLeft:int = 0;
        protected var m_responseRatio:JLabel;
        protected var m_firstCheckinLine:AssetPane;
        protected var m_secondCheckinLine:JLabel;
        protected var m_rewardSpriteAssets:Array;
        protected var m_rewardsPanel:JPanel;
        protected var m_collectButtonWidth:int;

        public function RollCallDialogView(_arg1:Array, _arg2:Dictionary, _arg3:String="", _arg4:String="", _arg5:int=0, _arg6:Function=null, _arg7:String="", _arg8:int=0, _arg9:String="", _arg10:Function=null, _arg11:String=""){
            this.m_timer = new Timer(1000);
            ExternalInterface.call("console.log", "creating rollCallDialogView");
            this.m_data = _arg1;
            this.m_mechData = (MechanicManager.getInstance().getMechanicInstance(_arg2["spawner"], "rollCall", MechanicManager.ALL) as RollCallDataMechanic);
            this.m_rewardData = (MechanicManager.getInstance().getMechanicInstance(_arg2["spawner"], "rollCallTieredDooberValue", MechanicManager.ALL) as TieredDooberMechanic);
            this.m_rewardSpriteAssets = new Array();
            addEventListener("onPostRollCall", this.onPostRollCall, false, 0, true);
            addEventListener("onCollectBonus", this.onCollectBonus, false, 0, true);
            addEventListener("onBuyCheckin", this.onBuyCheckin, false, 0, true);
            addEventListener("needMoreCash", this.needMoreCash, false, 0, true);
            super(_arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8, _arg9, _arg10, _arg11);
        }
        public function get nextRewardIndex():int{
            return (this.m_nextRewardIndex);
        }
        public function set nextRewardIndex(_arg1:int):void{
            this.m_nextRewardIndex = _arg1;
        }
        public function get checkinsLeft():int{
            return (this.m_checkinsLeft);
        }
        public function set checkinsLeft(_arg1:int):void{
            this.m_checkinsLeft = _arg1;
        }
        override protected function createTextArea():JPanel{
            var _local2:JPanel;
            var _local3:JPanel;
            var _local7:DisplayObject;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP, 5);
            if (((this.m_mechData.canPerformRollCall()) || (!(this.m_mechData.isRollCallComplete())))){
                _local2 = this.makeTopPane();
                _local3 = this.makeRewardPane();
            } else {
                _local3 = this.makeRewardCompletePane();
                _local7 = (new (m_assetDict["payroll_burst"])() as DisplayObject);
                _local1.addChild(_local7);
                _local7.x = 472;
                _local7.y = 360;
                _local7.width = this.m_collectButtonWidth;
            };
            var _local4:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
            var _local5:VectorListModel = new VectorListModel(this.m_data);
            var _local6:VerticalScrollingList = new VerticalScrollingList(this.m_assetDict, _local5, new RenameCellFactory(RollCallDialogCell, m_assetDict), 1, 3, LIST_WIDTH, LIST_HEIGHT, true);
            ASwingHelper.setEasyBorder(_local1, 10, 20, 0, 20);
            _local6.addEventListener(UIEvent.CHANGE_CREW_STATE, this.refreshStats, false, 0, true);
            _local4.append(_local6);
            _local1.appendAll(_local2, _local4, ASwingHelper.verticalStrut(-5), _local3, ASwingHelper.verticalStrut(15));
            return (_local1);
        }
        private function refreshStats(_arg1:UIEvent):void{
            this.updateResponseRatio();
            this.updateRewardPane();
        }
        protected function makeBorder():AssetPane{
            var _local1:Sprite = new Sprite();
            _local1.graphics.beginFill(EmbeddedArt.lightBlueTextColor);
            _local1.graphics.drawRoundRect(0, 0, 3, 38, 2, 2);
            _local1.graphics.endFill();
            var _local2:AssetPane = new AssetPane(_local1);
            return (_local2);
        }
        protected function makeResponsesPane():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP, -5);
            this.m_responseRatio = ASwingHelper.makeLabel(" ", EmbeddedArt.TITLE_FONT, 30, EmbeddedArt.lightOrangeTextColor);
            this.updateResponseRatio();
            var _local2:JLabel = ASwingHelper.makeLabel(ZLoc.t("Dialogs", "RollCall_response"), EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 12, EmbeddedArt.blueTextColor);
            _local1.appendAll(ASwingHelper.leftAlignElement(this.m_responseRatio), ASwingHelper.leftAlignElement(_local2));
            return (_local1);
        }
        private function updateResponseRatio():void{
            var _local3:Object;
            var _local4:int;
            var _local5:String;
            var _local1:RollCallDataMechanic = (MechanicManager.getInstance().getMechanicInstance(assetDict["spawner"], "rollCall", MechanicManager.ALL) as RollCallDataMechanic);
            var _local2:Array = _local1.getCrewState();
            this.m_friendsResponded = 0;
            for each (_local3 in _local2) {
                if (_local3.checkedIn == true){
                    this.m_friendsResponded++;
                };
            };
            _local4 = _local2.length;
            _local5 = ((String(this.m_friendsResponded) + "/") + String(_local4));
            this.m_responseRatio.setText(_local5);
            ASwingHelper.prepare(this.parent);
        }
        protected function makeTopPane():JPanel{
            var _local1:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT, 10);
            var _local2:AssetPane = new AssetPane((new (m_assetDict["payroll_icon_responses"])() as DisplayObject));
            var _local3:JPanel = this.makeResponsesPane();
            var _local4:AssetPane = new AssetPane((new (m_assetDict["payroll_icon_clock"])() as DisplayObject));
            var _local5:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP, -5);
            this.m_timerText = ASwingHelper.makeLabel(this.calcTimeLabel(), EmbeddedArt.TITLE_FONT, 30, EmbeddedArt.lightOrangeTextColor, JLabel.LEFT);
            var _local6:JLabel = ASwingHelper.makeLabel(ZLoc.t("Dialogs", "RollCall_timeRemaining"), EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 12, EmbeddedArt.blueTextColor);
            this.m_timerText.setPreferredWidth(230);
            this.m_timer.addEventListener(TimerEvent.TIMER, this.updateLabel, false, 0, true);
            this.m_timer.start();
            _local5.appendAll(ASwingHelper.leftAlignElement(this.m_timerText), ASwingHelper.leftAlignElement(_local6));
            _local1.appendAll(_local2, _local3, this.makeBorder(), _local4, _local5);
            return (_local1);
        }
        private function updateLabel(_arg1:TimerEvent):void{
            this.m_timerText.setText(this.calcTimeLabel());
        }
        protected function calcTimeLabel():String{
            var _local1:String;
            var _local2:Number = this.m_mechData.getRollCallTimeLeft();
            if (_local2 <= 0){
                this.m_timer.stop();
                this.close();
                if (m_closeCallback != null){
                    m_closeCallback(new GenericPopupEvent(GenericPopupEvent.SELECTED, GenericDialogView.YES));
                };
            };
            _local1 = CardUtil.formatTimeHours(_local2);
            return (_local1);
        }
        protected function generateRewardPane():JPanel{
            var rewardComplete:* = false;
            var tierObj:* = null;
            var checkinsNeeded:* = 0;
            var tierRewardObject:* = null;
            var key:* = null;
            var rewardAsset:* = null;
            var reward:* = null;
            var rewardPane:* = null;
            var tierName:* = null;
            var itemName:* = null;
            var tierAmount:* = NaN;
            var item:* = null;
            var itemLoader:* = null;
            var jp:* = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT, 0);
            var newRewardData:* = (MechanicManager.getInstance().getMechanicInstance(assetDict["spawner"], "rollCallTieredDooberValue", MechanicManager.ALL) as TieredDooberMechanic);
            var numTiers:* = newRewardData.getNumTiers();
            var i:* = 1;
            while (i < numTiers) {
                tierObj = newRewardData.getTierInfo(i);
                checkinsNeeded = tierObj.num;
                if (this.m_friendsResponded >= checkinsNeeded){
                    rewardComplete = true;
                    this.nextRewardIndex = 0;
                    this.checkinsLeft = 0;
                } else {
                    rewardComplete = false;
                    if ((((this.nextRewardIndex == 0)) || ((this.nextRewardIndex == i)))){
                        this.checkinsLeft = (checkinsNeeded - this.m_friendsResponded);
                        this.nextRewardIndex = i;
                    };
                };
                tierRewardObject = newRewardData.getTierRewardInfo(i);
                for (key in tierRewardObject) {
                    tierName = key.toString();
                    itemName = "";
                    tierAmount = 0;
                    if ((tierRewardObject[key] is Number)){
                        tierAmount = tierRewardObject[key];
                    } else {
                        itemName = tierRewardObject[key].name;
                        tierAmount = tierRewardObject[key].count;
                    };
                };
                rewardAsset = new Sprite();
                switch (tierName){
                    case "coin":
                        rewardAsset = (new (m_assetDict["payroll_reward_coin"])() as DisplayObject);
                        break;
                    case "xp":
                        rewardAsset = (new (m_assetDict["payroll_reward_xp"])() as DisplayObject);
                        break;
                    case "energy":
                        rewardAsset = (new (m_assetDict["payroll_reward_energy"])() as DisplayObject);
                        break;
                    case "item":
                        item = Global.gameSettings.getItemByName(itemName);
                        if (item){
                            itemLoader = LoadingManager.load(item.icon, Curry.curry(function (_arg1:JPanel, _arg2:Event):void{
                                if ((rewardAsset is DisplayObjectContainer)){
                                    (rewardAsset as DisplayObjectContainer).addChild(itemLoader.content);
                                    itemLoader.content.x = (itemLoader.content.x - (itemLoader.content.width / 2));
                                    itemLoader.content.y = (itemLoader.content.y - (itemLoader.content.height / 2));
                                };
                                ASwingHelper.prepare(_arg1);
                                _arg1.dispatchEvent(new UIEvent(UIEvent.REFRESH_DIALOG, "itemIconLoaded", true));
                            }, this));
                        };
                        break;
                };
                reward = new CrewRewardSprite(m_assetDict, rewardAsset, rewardComplete, tierAmount, checkinsNeeded);
                this.m_rewardSpriteAssets.push(reward);
                rewardPane = new AssetPane(reward);
                if (!rewardComplete){
                    ASwingHelper.setEasyBorder(rewardPane, 0, 15, 0, 15);
                } else {
                    ASwingHelper.setEasyBorder(rewardPane, 14, 0, 0, 15);
                };
                jp.append(ASwingHelper.bottomAlignElement(rewardPane));
                if (i != 3){
                    jp.append(new AssetPane((new (m_assetDict["payroll_rewardArrow"])() as DisplayObject)));
                };
                i = (i + 1);
            };
            ASwingHelper.prepare(jp);
            return (jp);
        }
        protected function makeRewardCompletePane():JPanel{
            var _local1:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT, 10);
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            var _local3:JPanel = this.makeResponsesPane();
            var _local4:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            var _local5:JPanel = this.generateRewardPane();
            _local4.appendAll(_local5);
            var _local6:String = TextFieldUtil.formatSmallCapsString(ZLoc.t("Dialogs", "RollCallCollect"));
            var _local7:JButton = new CustomButton(_local6, null, "GreenButtonUI");
            var _local8:ASFont = _local7.getFont();
            _local7.setFont(_local8.changeSize(TextFieldUtil.getLocaleFontSizeByRatio(_local8.getSize(), 1, [{
                locale:"de",
                ratio:0.9
            }, {
                locale:"id",
                ratio:0.6
            }, {
                locale:"es",
                ratio:0.9
            }, {
                locale:"fr",
                ratio:0.8
            }, {
                locale:"it",
                ratio:0.75
            }, {
                locale:"pt",
                ratio:0.85
            }, {
                locale:"tr",
                ratio:1
            }])));
            ASwingHelper.prepare(_local7);
            this.m_collectButtonWidth = _local7.getWidth();
            if (((Global.rollCallManager.canCollect(Global.player.uid, m_assetDict["spawner"])) && (!((this.nextRewardIndex == 1))))){
                _local7.addActionListener(this.collect, 0, true);
            } else {
                _local7.setEnabled(false);
            };
            _local2.appendAll(_local3, ASwingHelper.verticalStrut(10), _local7);
            _local1.appendAll(ASwingHelper.horizontalStrut(20), _local4, this.makeBorder(), _local2);
            return (_local1);
        }
        private function collect(_arg1:AWEvent):void{
            Global.rollCallManager.collect(Global.player.uid, m_assetDict["spawner"]);
            this.close();
            if (m_closeCallback != null){
                m_closeCallback(new GenericPopupEvent(GenericPopupEvent.SELECTED, GenericDialogView.NO));
            };
        }
        private function updateCheckinText():String{
            var _local3:Object;
            var _local4:LocalizationObjectToken;
            var _local5:String;
            var _local6:String;
            var _local7:String;
            var _local8:Number;
            var _local9:Item;
            var _local1:TieredDooberMechanic = (MechanicManager.getInstance().getMechanicInstance(assetDict["spawner"], "rollCallTieredDooberValue", MechanicManager.ALL) as TieredDooberMechanic);
            var _local2:Dictionary = _local1.getTierRewardInfo(this.m_nextRewardIndex);
            for (_local3 in _local2) {
                _local6 = _local3.toString();
                _local7 = "";
                _local8 = 0;
                if ((_local2[_local3] is Number)){
                    _local8 = _local2[_local3];
                } else {
                    _local7 = _local2[_local3].name;
                    _local8 = _local2[_local3].count;
                };
            };
            switch (_local6){
                case "coin":
                    _local4 = ZLoc.tk("Rewards", "Coins", "", _local8);
                    _local5 = ZLoc.t("Dialogs", "RollCall_togetCoins", {
                        amount:_local8.toString(),
                        Coin:_local4
                    });
                    break;
                case "xp":
                    _local4 = ZLoc.tk("Rewards", "XP", "", _local8);
                    _local5 = ZLoc.t("Dialogs", "RollCall_togetXP", {
                        amount:_local8.toString(),
                        XP:_local4
                    });
                    break;
                case "energy":
                    _local4 = ZLoc.tk("Rewards", "Energy", "", _local8);
                    _local5 = ZLoc.t("Dialogs", "RollCall_togetEnergy", {
                        amount:_local8.toString(),
                        Energy:_local4
                    });
                    break;
                case "item":
                    _local9 = Global.gameSettings.getItemByName(_local7);
                    _local5 = "";
                    if (_local9){
                        _local4 = _local9.getLocalizedObjectToken(_local8);
                        _local5 = ZLoc.t("Dialogs", "RollCall_togetItem", {
                            amount:_local8.toString(),
                            Item:_local4
                        });
                    };
                    break;
                default:
                    _local5 = "";
            };
            return (_local5);
        }
        private function updateRewardPane():void{
            this.m_rewardsPanel.removeAll();
            this.m_rewardsPanel.append(this.generateRewardPane());
            var _local1:LocalizationObjectToken = ZLoc.tk("Dialogs", "Checkin", "", this.m_checkinsLeft);
            var _local2:TextField = (this.m_firstCheckinLine.getAsset() as TextField);
            var _local3:TextFormat = _local2.getTextFormat();
            _local2.text = ZLoc.t("Dialogs", "RollCall_moreCheckins", {
                numCheckins:this.m_checkinsLeft,
                Checkin:_local1
            });
            _local2.setTextFormat(_local3);
            this.m_secondCheckinLine.setText(this.updateCheckinText());
            ASwingHelper.prepare(this.parent);
        }
        protected function makeRewardPane():JPanel{
            this.m_rewardsPanel = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
            this.m_rewardsPanel.append(this.generateRewardPane());
            ASwingHelper.setEasyBorder(this.m_rewardsPanel, 0, 20);
            var _local1:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            var _local3:LocalizationObjectToken = ZLoc.tk("Dialogs", "Checkin", "", this.m_checkinsLeft);
            this.m_firstCheckinLine = ASwingHelper.makeMultilineCapsText(ZLoc.t("Dialogs", "RollCall_moreCheckins", {
                numCheckins:this.m_checkinsLeft,
                Checkin:_local3
            }), 205, EmbeddedArt.TITLE_FONT, TextFormatAlign.RIGHT, 26, EmbeddedArt.lightOrangeTextColor);
            this.m_secondCheckinLine = ASwingHelper.makeLabel(TextFieldUtil.formatSmallCapsString(this.updateCheckinText()), EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 14, EmbeddedArt.blueTextColor, JLabel.RIGHT);
            _local2.appendAll(this.m_firstCheckinLine, this.m_secondCheckinLine);
            _local1.appendAll(_local2, ASwingHelper.horizontalStrut(10), this.makeBorder(), this.m_rewardsPanel);
            return (_local1);
        }
        override protected function createButtonPanel():JPanel{
            var _local1:JPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            return (_local1);
        }
        override public function close():void{
            if (this.m_timer){
                this.m_timer.stop();
            };
            super.close();
        }
        protected function needMoreCash(_arg1:Event):void{
            this.close();
            Global.ui.displayLightbox(false);
            UI.displayPopup(new GetCoinsDialog(ZLoc.t("Dialogs", "ImpulseMarketCash"), "", GenericDialogView.TYPE_YESNO, null, true), false);
        }
        protected function onBuyCheckin(_arg1:Event):void{
            var _local2:int = Global.gameSettings.getInt("rollCallSkipCheckinPrice", 30);
            countDialogViewAction("skip_for_cash", true, _local2);
        }
        protected function onPostRollCall(_arg1:Event):void{
            countDialogViewAction("post_roll_call");
        }
        protected function onCollectBonus(_arg1:Event):void{
            countDialogViewAction("collect_bonus");
        }

    }
}//package Modules.crew.ui 
